
package com.jf.cloud.live.service.impl;

import com.jf.cloud.common.database.dto.PageDTO;
import com.jf.cloud.common.database.util.PageUtil;
import com.jf.cloud.common.database.vo.PageVO;
import com.jf.cloud.common.exception.LuckException;
import com.jf.cloud.live.config.WxConfig;
import com.jf.cloud.live.constant.LiveInterfaceType;
import com.jf.cloud.live.constant.WxErrorCode;
import com.jf.cloud.live.dto.LiveRoomProdDTO;
import com.jf.cloud.live.mapper.LiveRoomProdMapper;
import com.jf.cloud.live.model.LiveProdStore;
import com.jf.cloud.live.model.LiveRoom;
import com.jf.cloud.live.model.LiveRoomProd;
import com.jf.cloud.live.service.LiveLogService;
import com.jf.cloud.live.service.LiveRoomProdService;
import com.jf.cloud.live.service.LiveRoomService;
import me.chanjar.weixin.common.error.WxErrorException;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 直播商品
 *
 * @author zz
 * @date 2022-08-17 17:09:06
 */
@Service
public class LiveRoomProdServiceImpl implements LiveRoomProdService {

    @Autowired
    private LiveRoomProdMapper liveRoomProdMapper;
    @Autowired
    private LiveLogService liveLogService;
    @Autowired
    private LiveRoomService liveRoomService;
    @Autowired
    private WxConfig wxConfig;

    @Override
    public PageVO<LiveRoomProd> page(PageDTO pageDTO, LiveRoomProdDTO liveRoomProdDTO) {
        return PageUtil.doPage(pageDTO, () -> liveRoomProdMapper.list(liveRoomProdDTO));
    }

    @Override
    public LiveRoomProd getByRoomProdId(Long roomProdId) {
        return liveRoomProdMapper.getByRoomProdId(roomProdId);
    }

    @Override
    public void save(LiveRoomProd liveRoomProd) {
        liveRoomProdMapper.save(liveRoomProd);
    }

    @Override
    public void update(LiveRoomProd liveRoomProd) {
        liveRoomProdMapper.update(liveRoomProd);
    }

    @Override
    public void deleteById(Long roomProdId) {
        liveRoomProdMapper.deleteById(roomProdId);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveBatchAndRoomId(LiveRoom liveRoom)  {
        // 1.校验今日可用次数并保存or修改商家次数记录
        liveLogService.checkNumsAndSaveLog(liveRoom.getShopId(), LiveInterfaceType.IMPORT_ROOM_PROD, "导入直播间商品");

        List<LiveProdStore> liveProdStores = liveRoom.getLiveProdStores();
        if(CollectionUtils.isEmpty(liveProdStores)){
            // 请选择商品
            throw new LuckException("请选择商品");
        }
        // 2.保存直播间商品关联表
        List<LiveRoomProd> liveRoomProds = new ArrayList<>();
        List<Integer> liveProdIds = new ArrayList<>();
        for (LiveProdStore liveProdStore : liveProdStores) {
            LiveRoomProd liveRoomProd = new LiveRoomProd();
            liveRoomProd.setRoomId(liveRoom.getRoomId());
            liveRoomProd.setProdStoreId(liveProdStore.getLiveProdStoreId());
            liveRoomProds.add(liveRoomProd);
            liveProdIds.add(liveProdStore.getGoodsId());
        }
        liveRoomProdMapper.saveBatch(liveRoomProds);


        // 4.导入商品
        try {
            wxConfig.getWxMaService().getLiveService().addGoodsToRoom(liveRoom.getRoomId(), liveProdIds);
        } catch (WxErrorException e) {
            if(e.getError().getErrorCode() == WxErrorCode.LIVE_ROOM_IS_NOT_EXIST.value()) {
                // 300022：此房间号不存在
                throw new LuckException("微信直播控制台不存在该房间");
            }else if(e.getError().getErrorCode() == WxErrorCode.LIVE_PROD_IS_NOT_EXIST.value()){
                // 300024：商品不存在
                throw new LuckException("微信直播控制台不存在该商品");
            }
            // 添加微信直播间商品失败
            throw new LuckException("添加微信直播间商品失败");
        }
    }


    @Override
    public List<LiveRoomProd> getByRoomId(Integer id) {
        return liveRoomProdMapper.getByRoomId(id);
    }

    @Override
    public void deleteBatchById(List<Long> ids) {
        liveRoomProdMapper.deleteBatchById(ids);
    }
}
